﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using OisLibrary;

namespace LanguageAnalyse
{
    public partial class TWLangExportToExcel : Form
    {
        public TWLangExportToExcel()
        {
            InitializeComponent();
        }

        private void TWLangExportToExcel_Load(object sender, EventArgs e)
        {

        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            string xmlDataPath = txtXmlData.Text.Trim();
            string filename = "";

            //裝載模塊
            List<Apmodel> listModel = LoadModel(xmlDataPath);

            //裝載語言

            List<LangItem> LangItemListTW = new List<LangItem>();
            PublicClass.LoadLanguage(LangItemListTW, System.IO.Path.Combine(xmlDataPath, "Languages/zh-tw.xml"));


            var result = from lang in LangItemListTW
                         join m in listModel on lang.pgid equals m.pgid
                         select new LangTWList { TwName = lang.lgvalue };


            List<LangTWList> listLang = result.ToList<LangTWList>();


            System.Text.RegularExpressions.Regex reg=new System.Text.RegularExpressions.Regex("^[a-zA-Z0-9-_]{1,}$");



            for (int i = listLang.Count-1; i >= 0; i--)
            {
                string value=listLang[i].TwName;
                if(reg.IsMatch(value))
                {
                    listLang.RemoveAt(i);
                    continue;
                }

                listLang[i].TwName = removeSpecialFlag(listLang[i].TwName);
                listLang[i].TwName = removeSpecialFlag(listLang[i].TwName);
                listLang[i].TwName = removeSpecialFlag(listLang[i].TwName);

            }




            



            var result2 = listLang.GroupBy(m => m.TwName).OrderBy(m=>m.Key.Length).ToList();



            for (int i = result2.Count - 1; i >= 0; i--)
            {
                if (result2[i].Key.Contains("單號") && result2[i].Key.Length<=3)
                {
                    removeSpecialFlag(result2[i].Key);
                }

            }

            object[,] data = new object[result2.Count + 1, 1];

            data[0, 0] = "繁体";

            for (int i = 0; i < result2.Count; i++)
            {
                data[i + 1, 0] = result2[i].Key;
            }

            ExcelOpt.TableToExcel(data, txtExcel.Text, 1);
 

        }
 





        private List<Apmodel> LoadModel(string xmlDataPath)
        {
            List<Apmodel> listModel = new List<Apmodel>();
            string filename = System.IO.Path.Combine(xmlDataPath, "apmodel.xml");

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filename);
            for (int i = 1; i < xmlDoc.ChildNodes[1].ChildNodes.Count; i++)
            {
                XmlNode row = xmlDoc.ChildNodes[1].ChildNodes[i];
                string pid = row.SelectSingleNode("pgid").InnerText;
                string name = row.SelectSingleNode("name").InnerText;
                listModel.Add(new Apmodel { pgid = pid, name = name });
            }
            return listModel;
        }

        private string removeSpecialFlag(string value)
        {
            string valueNew=value.Trim();

            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("\r$");
            if (reg.IsMatch(valueNew))
            {
                valueNew= reg.Replace(valueNew, "");
            }

            if (valueNew.EndsWith(":") || valueNew.EndsWith("：") || valueNew.EndsWith(":") || valueNew.EndsWith(" ") || valueNew.EndsWith(":"))
            {
                valueNew = valueNew.Substring(0, value.Length - 1);
            }
            return valueNew;
        }


    }

    public class LangTWList
    {
        public string TwName { set; get; }
    }

}
